package view;

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;

import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoManager;

import controller.EditController;
import controller.FileController;
import controller.FormatController;


/*
 * JanelaPrincipal.java
 *
 * Created on 09/02/2013, 15:51:03
 *
/**
 *
 * @author Larissa
 */
public class JanelaPrincipal extends javax.swing.JFrame {

    /** Creates new form JanelaPrincipal */
     FormatController formatController;
     FileController fileController;
     EditController editController;
     MainPanel painelPrincipalExtendido;
     public JanelaPrincipal() {
        super("RPad++");
        initComponents();
        this.setLocation(20, 20);
        this.setSize(new Dimension(820,600));
        inicializaInterface();
        
        fileController = new FileController();
        formatController = new FormatController();
        editController = new EditController();
        
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        painelPrincipal = new javax.swing.JPanel();
        fileTabbedPanel = new javax.swing.JTabbedPane();
        panelFile = new javax.swing.JPanel();
        toolBarFile = new javax.swing.JToolBar();
        buttonNew = new javax.swing.JButton();
        buttonOpen = new javax.swing.JButton();
        buttonSaveNormal = new javax.swing.JButton();
        buttonSaveAs = new javax.swing.JButton();
        buttonExit = new javax.swing.JButton();
        panelEdit = new javax.swing.JPanel();
        toolBarEdit1 = new javax.swing.JToolBar();
        toolBarEdit2 = new javax.swing.JToolBar();
        toolBarEdit3 = new javax.swing.JToolBar();
        toolBarEdit4 = new javax.swing.JToolBar();
        panelFormat = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jLabel4 = new javax.swing.JLabel();
        panelView = new javax.swing.JPanel();
        toolBarView = new javax.swing.JToolBar();
        buttonRulerBar = new javax.swing.JButton();
        painelAuxiliar = new javax.swing.JTabbedPane();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        fileTabbedPanel.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N

        panelFile.setMaximumSize(new java.awt.Dimension(810, 32767));
        panelFile.setMinimumSize(new java.awt.Dimension(100, 10));
        panelFile.setPreferredSize(new java.awt.Dimension(810, 102));

        toolBarFile.setRollover(true);
        toolBarFile.setMinimumSize(new java.awt.Dimension(60, 10));

        buttonNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/new.png"))); // NOI18N
        buttonNew.setText("New");
        buttonNew.setFocusable(false);
        buttonNew.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonNew.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonNewActionPerformed(evt);
            }
        });
        toolBarFile.add(buttonNew);

        buttonOpen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/open.png"))); // NOI18N
        buttonOpen.setText("Open");
        buttonOpen.setFocusable(false);
        buttonOpen.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonOpen.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOpenActionPerformed(evt);
            }
        });
        toolBarFile.add(buttonOpen);

        buttonSaveNormal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/save.png"))); // NOI18N
        buttonSaveNormal.setText("Save");
        buttonSaveNormal.setFocusable(false);
        buttonSaveNormal.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonSaveNormal.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonSaveNormal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveNormalActionPerformed(evt);
            }
        });
        toolBarFile.add(buttonSaveNormal);

        buttonSaveAs.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/saveAs.png"))); // NOI18N
        buttonSaveAs.setText("Save As");
        buttonSaveAs.setFocusable(false);
        buttonSaveAs.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonSaveAs.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonSaveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveAsActionPerformed(evt);
            }
        });
        toolBarFile.add(buttonSaveAs);

        buttonExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/close.png"))); // NOI18N
        buttonExit.setText("Exit");
        buttonExit.setFocusable(false);
        buttonExit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonExit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonExitActionPerformed(evt);
            }
        });
        toolBarFile.add(buttonExit);

        javax.swing.GroupLayout panelFileLayout = new javax.swing.GroupLayout(panelFile);
        panelFile.setLayout(panelFileLayout);
        panelFileLayout.setHorizontalGroup(
            panelFileLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFileLayout.createSequentialGroup()
                .addComponent(toolBarFile, javax.swing.GroupLayout.PREFERRED_SIZE, 798, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 88, Short.MAX_VALUE))
        );
        panelFileLayout.setVerticalGroup(
            panelFileLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFileLayout.createSequentialGroup()
                .addComponent(toolBarFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        fileTabbedPanel.addTab("File", panelFile);

        toolBarEdit1.setRollover(true);

        toolBarEdit2.setRollover(true);

        toolBarEdit3.setRollover(true);

        toolBarEdit4.setRollover(true);

        javax.swing.GroupLayout panelEditLayout = new javax.swing.GroupLayout(panelEdit);
        panelEdit.setLayout(panelEditLayout);
        panelEditLayout.setHorizontalGroup(
            panelEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelEditLayout.createSequentialGroup()
                .addComponent(toolBarEdit1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(toolBarEdit2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(toolBarEdit3, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(toolBarEdit4, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE))
        );
        panelEditLayout.setVerticalGroup(
            panelEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolBarEdit1, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
            .addComponent(toolBarEdit2, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
            .addComponent(toolBarEdit3, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
            .addComponent(toolBarEdit4, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
        );

        fileTabbedPanel.addTab("Edit", panelEdit);

        jToolBar1.setRollover(true);

        fileTabbedPanel.addTab("Format", panelFormat);

        toolBarView.setRollover(true);

        buttonRulerBar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/ruler.png"))); // NOI18N
        buttonRulerBar.setText("RulerBar");
        buttonRulerBar.setFocusable(false);
        buttonRulerBar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonRulerBar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonRulerBar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRulerBarActionPerformed(evt);
            }
        });
        toolBarView.add(buttonRulerBar);

        javax.swing.GroupLayout panelViewLayout = new javax.swing.GroupLayout(panelView);
        panelView.setLayout(panelViewLayout);
        panelViewLayout.setHorizontalGroup(
            panelViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolBarView, javax.swing.GroupLayout.DEFAULT_SIZE, 886, Short.MAX_VALUE)
        );
        panelViewLayout.setVerticalGroup(
            panelViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolBarView, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
        );

        fileTabbedPanel.addTab("View", panelView);

        painelAuxiliar.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        painelAuxiliar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        painelAuxiliar.setMaximumSize(new java.awt.Dimension(795, 32767));
        painelAuxiliar.setName("cboHighlight"); // NOI18N

        javax.swing.GroupLayout painelPrincipalLayout = new javax.swing.GroupLayout(painelPrincipal);
        painelPrincipal.setLayout(painelPrincipalLayout);
        painelPrincipalLayout.setHorizontalGroup(
            painelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fileTabbedPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addGroup(painelPrincipalLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(painelAuxiliar, javax.swing.GroupLayout.PREFERRED_SIZE, 795, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(86, 86, 86))
        );
        painelPrincipalLayout.setVerticalGroup(
            painelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelPrincipalLayout.createSequentialGroup()
                .addComponent(fileTabbedPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(painelAuxiliar, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(painelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(painelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //**Muda: Tem que comentar uma das duas linhas abaixo para executar
    private void buttonOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOpenActionPerformed
    	fileController.openFileSingle(this, (MainPanel)this.painelAuxiliar.getSelectedComponent(), painelAuxiliar); //Single	
    }//GEN-LAST:event_buttonOpenActionPerformed

    private void buttonSaveNormalActionPerformed(java.awt.event.ActionEvent evt) {                                             
        fileController.saveNormal((MainPanel)this.painelAuxiliar.getSelectedComponent(), painelAuxiliar); //corrigir
    }                                            
    
    private void buttonSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveAsActionPerformed
        fileController.saveAs((MainPanel)this.painelAuxiliar.getSelectedComponent(), painelAuxiliar); //corrigir
    }//GEN-LAST:event_buttonSaveAsActionPerformed

    private void buttonRulerBarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRulerBarActionPerformed
        formatController.ocultaReguaController((MainPanel)this.painelAuxiliar.getSelectedComponent());
    }//GEN-LAST:event_buttonRulerBarActionPerformed

    //**Muda: Tem que comentar uma das duas linhas abaixo para executar
    private void buttonNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonNewActionPerformed
       fileController.newFileSingle((MainPanel)this.painelAuxiliar.getSelectedComponent(),painelPrincipalExtendido, painelAuxiliar, "Untitled"); //single 
    }//GEN-LAST:event_buttonNewActionPerformed
    
    //**Muda: Tem que comentar uma das duas linhas abaixo para executar
    private void buttonExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonExitActionPerformed
        fileController.exitSingle(painelPrincipalExtendido, painelAuxiliar);
    }//GEN-LAST:event_buttonExitActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                new JanelaPrincipal().setVisible(true);
                }
        });
    }
    
    
    private javax.swing.JButton buttonExit;
    private javax.swing.JButton buttonNew;
    private javax.swing.JButton buttonOpen;
    private javax.swing.JButton buttonRulerBar;
    private javax.swing.JButton buttonSaveAs;
    private javax.swing.JButton buttonSaveNormal;
    private javax.swing.JTabbedPane fileTabbedPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTabbedPane painelAuxiliar;
    private javax.swing.JPanel painelPrincipal;
    private javax.swing.JPanel panelEdit;
    private javax.swing.JPanel panelFile;
    private javax.swing.JPanel panelFormat;
    private javax.swing.JPanel panelView;
    private javax.swing.JToolBar toolBarEdit1;
    private javax.swing.JToolBar toolBarEdit2;
    private javax.swing.JToolBar toolBarEdit3;
    private javax.swing.JToolBar toolBarEdit4;
    private javax.swing.JToolBar toolBarFile;
    private javax.swing.JToolBar toolBarView;
    // End of variables declaration//GEN-END:variables
    
    private void inicializaInterface() {
        
        painelPrincipalExtendido = new MainPanel();
        this.painelAuxiliar.add(painelPrincipalExtendido);
        this.painelAuxiliar.setTitleAt(0, "Untitled");
        this.painelAuxiliar.setIconAt(0, new ImageIcon((getClass().getResource("/imagens/fileUntitled.png"))));
    }
   
}
